package com.FengChaoSystem.service.MessageService;

import com.FengChaoSystem.dao.MessageDao;
import com.FengChaoSystem.pojo.Message;
import com.FengChaoSystem.service.MessageImpl;
import com.FengChaoSystem.untils.SqlSessionFactoryUtils;
import com.Mybatis.session.SqlSession;
import com.Mybatis.session.SqlSessionFactory;

import java.util.List;

public class MessageService implements MessageImpl {
    SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<Message> selectAllHasReadMessages(Message message) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MessageDao mapper = sqlSession.getMapper(MessageDao.class);
        List<Message> messages = mapper.selectAllHasReadMessages(message);
        sqlSession.close();
        return messages;
    }

    @Override
    public void addMessage(Message message) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MessageDao mapper = sqlSession.getMapper(MessageDao.class);
        mapper.addMessage(message);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateHasRead(Message target) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MessageDao mapper = sqlSession.getMapper(MessageDao.class);
        mapper.updateHasRead(target);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Message> getMessage(Message message) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MessageDao mapper = sqlSession.getMapper(MessageDao.class);
        List<Message> result = mapper.getMessage(message);
        sqlSession.close();
        return result;
    }
}
